package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

import unit.collisions.WalkerToPanelCollision;
import unit.collisions.WalkerToWalkerCollision;
import unit.integrator.WalkerGenerator;

import com.golden.gamedev.Game;
import com.golden.gamedev.GameLoader;
import com.golden.gamedev.object.Background;
import com.golden.gamedev.object.CollisionManager;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.SpriteGroup;
import com.golden.gamedev.object.background.ColorBackground;


public class GameMain extends Game {

	//{ distribute = true; } - allows a game to finish and no longer show rendering
    Background   background;
    SpriteGroup  PLAYER_GROUP, WALKER1_GROUP, WALKER2_GROUP,
                 PANEL_GROUP;
    Sprite hero,villian;
    WalkerGenerator generator = new WalkerGenerator();
    CollisionManager collision;
    CollisionManager collision2;


    public void initResources() {
    	//hero = new Sprite(getImage("Bird.gif"), 0, 0);
        //hero.setSpeed(.05, .1);
        
       // villian = new Sprite(getImage("projectile.png"), 150, 200);
        //villian.setSpeed(-.08, -.04);
    	
    	
    	
        background = new ColorBackground(Color.BLUE, 1024, 768);

        PLAYER_GROUP      = new SpriteGroup("Player Group");
       // PLAYER_GROUP.add(new Sprite(getImage("Cat.gif"), 150, 200));

        PANEL_GROUP = new SpriteGroup("Panel Group");
       // PLAYER_SHOT_GROUP.add(villian);

        WALKER1_GROUP = new SpriteGroup("Good Walker Group");
        //WALKER_GROUP.add(hero);
        
        WALKER2_GROUP = new SpriteGroup("Bad Walker Group");

        PLAYER_GROUP.setBackground(background);
        PANEL_GROUP.setBackground(background);
        WALKER1_GROUP.setBackground(background);
        WALKER2_GROUP.setBackground(background);
        
        PANEL_GROUP = generator.randomlyGenerateAllPanels();
        
        collision = new WalkerToPanelCollision();
        collision.setCollisionGroup(WALKER2_GROUP, PANEL_GROUP);
        
        collision2 = new WalkerToWalkerCollision();
        collision2.setCollisionGroup(WALKER1_GROUP, WALKER2_GROUP);
        
    }

    @Override
    public void update(long elapsedTime) {
        background.update(elapsedTime);
        if(keyPressed(KeyEvent.VK_NUMPAD6)){
        	WALKER1_GROUP.add(generator.randomlyGenerateWalker());
        }
        if(keyPressed(KeyEvent.VK_NUMPAD4)){
        	Sprite evilSprite = generator.randomlyGenerateWalker();
        	evilSprite.setSpeed(evilSprite.getHorizontalSpeed()*-1, 0);
        	WALKER2_GROUP.add(evilSprite);
        }
        
        WALKER1_GROUP.update(elapsedTime);
        PANEL_GROUP.update(elapsedTime);
        WALKER2_GROUP.update(elapsedTime);
        collision.checkCollision();
        collision2.checkCollision();
    }

    @Override
    public void render(Graphics2D g) {
        background.render(g);

        PLAYER_GROUP.render(g);
        PANEL_GROUP.render(g);
        WALKER1_GROUP.render(g);
        WALKER2_GROUP.render(g);
    }


    public static void main(String[] args) {
        GameLoader game = new GameLoader();
        
        com.golden.gamedev.Game gamer = new GameMain();
        game.setup(gamer, new Dimension(640,480), false);
        game.start();
    }


}